<?php

class Utility_ArrayFunctions {
                         
    static public function array_insert($arr, $mix_key_target, $arr_insert, $arr_options = array()) {

        $i = 0;
        foreach($arr as $mix_key => $mix_value) {
            if($mix_key_target == $mix_key) {
                break;
            }
            $i++;
        }

        $int_position = $i+1;
        if(isset($arr_options['placement'])) {
            $str_placement = strtolower($arr_options['placement']);
            if($str_placement == 'before') {
                $int_position--;
            }
        }

        $arr_first = array_splice($arr, 0, $int_position);
        $arr = array_merge($arr_first, $arr_insert, $arr);

        return $arr;
    }

    /**
     * From an list of associative arrays, generate a new array which only has
     * key-value pairs.  Quite useful for simplifying and preparing a structure
     * for input select options.
     */
    static public function key_values($arr_data, $str_key = 'id', $str_value = 'label') {

        $arr_output = array();
        foreach($arr_data as $arr) {
            if(isset($arr[$str_key])) {
                $arr_output[$arr[$str_key]] = isset($arr[$str_value]) ? $arr[$str_value] : null;
            }
        }

        return $arr_output;
    }

    static public function index_by($arr_data, $str_key)
    {
        $arr_output = array();

        //if (is_array($arr_data)) {
            foreach ($arr_data as $arr) {
                if (isset($arr[$str_key])) {
                    $arr_output[$arr[$str_key]] = $arr;
                }
            }
        //}

        return $arr_output;
    }

    static public function group_values($arr, $str_group_key = 'id', $mix_item = null) {

        $arr_output = array();

        //if(is_array($arr)) {

            foreach($arr as $mix) {

                if(is_array($mix) && isset($mix[$str_group_key])) {

                    $str_key = $mix[$str_group_key];
                    
                    if (!isset($arr_output[$str_key])) {
                        $arr_output[$str_key] = array();
                    }
                    
                    if($mix_item === true) {
                        unset($mix[$str_group_key]);
                    } else if(is_string($mix_item)) {
                        $mix = isset($mix[$mix_item]) ? $mix[$mix_item] : null;
                    }
                    
                    $arr_output[$str_key][] = $mix;

                }

            }

        //}

        return $arr_output;
    }

    static public function values_matching_keys($arr, $str_key_pattern) {

        $arr_output = array();

        if(is_array($arr) && is_string($str_key_pattern)) {
            foreach($arr as $str_key => $mix_value) {
                if(stripos($str_key, $str_key_pattern) !== false) {
                    $arr_output[$str_key] = $mix_value;
                }
            }
        }

        return $arr_output;

    }

    /**
     * Removes any elements with keys that match the supplied pattern.  This
     * function does not edit the original array; instead, it will make a copy
     * of the array and its elements with those elements which do not match
     * the supplied pattern.
     *
     * @param array $arr The array to remove elements from
     * @param string $str_key_pattern The pattern to match against keys
     * @return array
     */
    static public function remove_elements_matching_keys($arr, $str_key_pattern) {

        $arr_output = array();

        if(is_array($arr) && is_string($str_key_pattern)) {
            foreach($arr as $str_key => $mix_value) {
                if(stripos($str_key, $str_key_pattern) === false) {
                    $arr_output[$str_key] = $mix_value;
                }
            }
        }

        return $arr_output;

    }
    
    static public function remove_elements_matching_key_pattern($arr, $str_pattern) {
        $arr_output = array();

        if(is_array($arr) && is_string($str_pattern)) {
            foreach($arr as $str_key => $mix_value) {
                if(preg_match($str_pattern, $str_key) === 0) {
                    $arr_output[$str_key] = $mix_value;
                }
            }
        }

        return $arr_output;
    }

    static public function index_of_key($arr, $str_key_to_index) {

        $int_index = null;

        $i = 0;
        foreach($arr as $str_key => $mix) {
            if($str_key_to_index == $str_key) {
                $int_index = $i;
            }
            $i++;
        }

        return $int_index;
    }
    
    static public function get_values_matching_pattern($arr, $str_pattern) {
    
        $arr_output = array();
    
        if(is_array($arr) && is_string($str_pattern)) {
            foreach($arr as $str_key => $mix_value) {
                $str_value = (string) $mix_value;
                if(preg_match($str_pattern, $str_value) !== 0) {
                    $arr_output[$str_key] = $mix_value;
                }
            }
        }

        return $arr_output;
    }

    static public function implode($arr, $str_delimiter = '', $str_separator = null, $arr_options = array()) {
        $str_output = '';

        $str_array_key = isset($arr_options['array_key']) ? $arr_options['array_key'] : null;

        if(is_array($arr)) {

            $arr_temp = array();

            foreach($arr as $str_key => $mix_value) {

                $str_value = (string) (is_array($mix_value) && !is_null($str_array_key)) ? $mix_value[$str_array_key] : $mix_value;

                if(!is_null($str_separator)) {
                    $str_value = $str_key.$str_separator.$str_value;
                }

                $arr_temp[] = $str_value;

            }

            $str_output = implode($str_delimiter, $arr_temp);
        }

        return $str_output;
    }
    
    static public function explode_lists($arr) {
        
        $arr_temp = array();
        foreach($arr as $str_key => $mix_value) {
            $arr_temp[$str_key] = self::list_to_array($mix_value);
        }
        
        return $arr_temp;
    }
    
    static public function list_to_array($mix, $str_delimiter = ',') {
        
        $arr_temp = array();
        if(is_string($mix)) {
            $mix = str_replace(' ', '', strtolower($mix));
            if(!empty($mix)) {
                $arr_temp = array($mix);
                if((strpos($mix, $str_delimiter) !== false)) {
                    $arr_temp = explode($str_delimiter, $mix);
                }
            }
        } else if(is_array($mix)) {
            $arr_temp = $mix;
        }
        
        return $arr_temp;
    }

    static public function join_on_key($arr1, $arr2, $str_join_key = 'id') {

        $arr_merged = array();

        $arr_indexes = array_keys($arr1);
        foreach($arr_indexes as $i) {

            if(isset($arr1[$i][$str_join_key]) && isset($arr2[$i][$str_join_key])) {
                $str_join_value1 = $arr1[$i][$str_join_key];
                $str_join_value2 = $arr2[$i][$str_join_key];

                if($str_join_value1 == $str_join_value2) {

                    $arr_merged[$i] = array_merge($arr2[$i], $arr1[$i]);
                }

            }

        }

        return $arr_merged;
    }

    static public function remove_nulls($arr_data) {
        $arr = array();
        foreach($arr_data as $str_key => $mix) {
            if(!is_null($mix)) {
                $arr[$str_key] = $mix;
            }
        }
        return $arr;
    }
    
    static public function count_positive(&$arr) {
        
        $int_sum = 0;
        if(is_array($arr)) {
            foreach($arr as $mix) {
                if(is_numeric($mix) && ($mix > 0)) {
                    $int_sum++;
                }
            }
        }
        
        return $int_sum;
    }
    
    static public function count_negative(&$arr) {
        
        $int_sum = 0;
        if(is_array($arr)) {
            foreach($arr as $mix) {
                if(is_numeric($mix) && ($mix < 0)) {
                    $int_sum++;
                }
            }
        }
        
        return $int_sum;
    }

    static public function sample($arr, $int_size = 5) {

        $arr_output = array();

        $i = 0;
        $int_length = count($arr);
        while($i < $int_length){

            $num_sum = 0;
            $int_count = 0;
            for($j = $i; $j < ($i+$int_size); $j++) {
                if(isset($arr[$j])) {
                    $num_sum += $arr[$j];
                    $int_count++;
                }

            }

            $arr_output[$i] = $num_sum / $int_count;

            $i = $i + $int_size;
        }

        return $arr_output;
    }
    
}
